package DoubleList;


import java.util.Stack;


public class ListDemo{
    public static void main(String[] args) {
        DoubleLikedList s1 = new DoubleLikedList();
        s1.addOrder(1,"孙悟空","齐天大圣");
        s1.addOrder(3,"张三丰","一眉道人");
        s1.addOrder(5,"张yi丰","一眉道人");
        s1.addOrder(2,"赵云","常山赵子龙");
        s1.addOrder(4,"张er丰","一眉道人");
        s1.addOrder(6,"张yi丰","一眉道人");

        s1.delete(2);
        s1.update(new heroNode2(2,"赵云","常山赵子龙"));

        DoubleLikedList.show(s1.getHead());
    }
}

class DoubleLikedList{
   heroNode2 head = new heroNode2(0,"1","2");

    public heroNode2 getHead() {
        return head;
    }

    // 删除节点
    public void delete(int no){

        heroNode2 temp = head.next;
        while(true){
            if(temp == null){
                System.out.println("节点不存在,无法删除");
                break;
            }

            if(temp.no == no){
                temp.pre.next = temp.next;
                if (temp.next != null) {// 不为空,才有pre
                    temp.next.pre = temp.pre;
                }
                break;
            }
            temp = temp.next;
        }
    }

    // 更新节点
    public void update(heroNode2 newHeroNode){
        // 链表为空,直接插入
        if(head.next == null){
            head.next = newHeroNode;
            return;
        }
        // 链表非空,遍历
        heroNode2 temp = head.next;
        while(true){
            if(temp.no == newHeroNode.no){
                temp.name = newHeroNode.name;
                temp.nickname = newHeroNode.nickname;
                break;
            }

            if(temp.next == null){
                temp.next = newHeroNode;
                break;
            }
            temp = temp.next;
        }

    }

    // 添加节点按照no有序插入
    public void addOrder(int no,String name,String nickname){
        heroNode2 newNode = new heroNode2(no,name,nickname);
        heroNode2 tmp = head;
        boolean flag = false;// flag 标志添加的节点是否存在
        // 链表非空,循环遍历
        while(true){
            if(tmp.next == null){// 节点为空,赋值为newNode
                tmp.next = newNode;
                newNode.pre = tmp;
                break;
            }

            if(tmp.next.no > newNode.no){
                tmp.next.pre = newNode;
                newNode.next = tmp.next;
                tmp.next = newNode;
                newNode.pre = tmp;
                break;
            }else if(tmp.next.no == newNode.no){// no相等表示英雄重复,抛出异常
                flag = true;
                break;
            }
            tmp = tmp.next;
        }
        if(flag){
            throw new RuntimeException();
        }
    }

    // 添加节点到双向链表
    public void addNode(int no,String name,String nickname){
        heroNode2 newNode = new heroNode2(no,name,nickname);

        // head 节点不能动,添加一个辅助节点,遍历链表到最后进行添加
        heroNode2 tmp = head;
        while(tmp.next != null){
            tmp = tmp.next;
        }
        tmp.next = newNode;
        newNode.pre = tmp;
    }

    // 显示链表,遍历
    public static void show(heroNode2 head){
        heroNode2 tmp = head.next;
        while(tmp != null){
            System.out.println(tmp);
            tmp = tmp.next;
        }
    }
}


// 英雄节点
class heroNode2{
    public int no;
    public String name;
    public String nickname;//昵称
    public heroNode2 next;
    public heroNode2 pre;

    public heroNode2() {
    }

    public heroNode2(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    // 重写toString方法
    @Override
    public String toString() {
        return "heroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname+
                '}';
    }

}